package org.ljy.rabbitextensionsdemo.controller;


import jakarta.annotation.Resource;
import org.ljy.rabbitextensionsdemo.constant.Constant;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageDeliveryMode;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RequestMapping("/produce")
@RestController
public class ProduceController {

    @Resource(name = "rabbitTemplate")
    private RabbitTemplate rabbitTemplate;

    @Resource(name = "confirmRabbitTemplate")
    private RabbitTemplate confirmRabbitTemplate;

    @Resource(name = "transRabbitTemplate")
    private RabbitTemplate transRabbitTemplate;


    //消息确认
    @RequestMapping("/ack")
    public String ack() {
        rabbitTemplate.convertAndSend(Constant.ACK_EXCHANGE,"ack","这是一条ack消息");
        return "消息发送成功";
    }


    //持久化
    @RequestMapping("/pres")
    public String pres() {
        Message message = new Message("我是非持久消息".getBytes(),new MessageProperties());

        //1.消息非持久化
        message.getMessageProperties().setDeliveryMode(MessageDeliveryMode.NON_PERSISTENT);
        //2.消息持久化
        //message.getMessageProperties().setDeliveryMode(MessageDeliveryMode.PERSISTENT);
        rabbitTemplate.convertAndSend(Constant.PRE_EXCHANGE,"pres",message);
        return "非持久消息发送成功";
    }



    //confirm确认模式
    @RequestMapping("/confirm")
    public String confirm() {
        CorrelationData correlationData  =new CorrelationData("1");//设置消息的id
        confirmRabbitTemplate.convertAndSend(Constant.CONFIRM_EXCHANGE,"confirm1","我是一条confirm test",correlationData);
        return "confirm";
    }


    //retry模式
    @RequestMapping("/retry")
    public String retry() {
        //System.out.println("我进来了");
        rabbitTemplate.convertAndSend(Constant.RETRY_EXCHANGE,"retry","我是retry ……");
        return "retry";
    }


    //ttl
    @RequestMapping("/ttl")
    public String ttl() {
        rabbitTemplate.convertAndSend(Constant.TTL_EXCHANGE,"ttl","a ttl test",message -> {
            message.getMessageProperties().setExpiration("10000");//设置10秒后过期
            return message;
        });

        rabbitTemplate.convertAndSend(Constant.TTL_EXCHANGE,"ttl","a ttl test",message -> {
            message.getMessageProperties().setExpiration("20000");//设置10秒后过期
            return message;
        });
        return "ttl";
    }

    @RequestMapping("/ttl2")
    public String ttl2() {
        rabbitTemplate.convertAndSend(Constant.TTL_EXCHANGE,"ttl2","a ttl2");
        return "ttl2";
    }

    //dlx死信队列
    @RequestMapping("/normal")
    public String dlx() {
        int maxLen = 12;
        for(int i=0;i<maxLen;i++) {
            rabbitTemplate.convertAndSend(Constant.NORMAL_EXCHANGE,"normal","normal and dlx");
        }
        return "normal";
    }

    @RequestMapping("/delay")
    public String delay() {
        rabbitTemplate.convertAndSend(Constant.NORMAL_EXCHANGE,"normal","这是一条延迟消息",message -> {
            message.getMessageProperties().setExpiration("10000");//设置10秒后过期
            return message;
        });
        System.out.println("消息发送时间："+new Date());
        return "delay";
    }


    @RequestMapping("/delay2")
    public String delay2() {
        rabbitTemplate.convertAndSend(Constant.NORMAL_EXCHANGE,"normal","这是一条延迟消息",message -> {
            message.getMessageProperties().setExpiration("10000");//设置10秒后过期
            return message;
        });
        System.out.println("消息发送时间："+new Date());
        return "delay";
    }


//    @Transactional
//    @RequestMapping("/trans")
//    public String trans() {
//        transRabbitTemplate.convertAndSend("",Constant.TRANS_QUEUE,"第一条消息");
//        //int a=9/0;//模拟发送异常
//        transRabbitTemplate.convertAndSend("",Constant.TRANS_QUEUE,"第二条消息");
//        return "trans";
//    }



    @Transactional
    @RequestMapping("/trans")
    public String trans() {
        rabbitTemplate.convertAndSend("",Constant.TRANS_QUEUE,"第一条消息");
        int a=9/0;//模拟发送异常
        rabbitTemplate.convertAndSend("",Constant.TRANS_QUEUE,"第二条消息");
        return "trans";
    }


    @RequestMapping("/qos")
    public String Qos() {
        for(int i=0;i<20;i++) {
            rabbitTemplate.convertAndSend(Constant.QOS_EXCHANGE,"qos","a qos test"+i);
        }
        return "qos";
    }



}
